package net.aizhuoyue.conrtoller;

import javax.validation.Valid;

import org.hibernate.annotations.Source;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.aizhuoyue.entity.Student;
import net.aizhuoyue.service.StudentService;

/**
 * @类名: StudentController   
 * @描述: 学生控制类  
 * @作者: 宋利军 
 * @时间: 2018年5月4日上午8:29:00  
 * @Copyright: 2018 http://blog.aizhuoyue.net.
 */
@RestController
@RequestMapping("/student")
public class StudentController {
	@Source
	private StudentService studentService;
	/**
	 * @描述方法:实体前要加@Valid 假如字段验证不通过，信息绑定到后面定义的BindingResult
	 */
	@PostMapping(value="/add")
	public String add(@Valid Student student,BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return bindingResult.getFieldError().getDefaultMessage();
		}else{
			studentService.add(student);
		}
		return "添加成功！";
	}
}
